package com.bjpowernode.crm.workbench.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.workbench.domain.*;
import com.bjpowernode.crm.workbench.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * ClassName:ClueServiceImpl
 * Package:com.bjpowernode.crm.workbench.service
 * Date:2022/6/13 11:12
 * Description:
 * Author:zsm@zhangshuming.com
 */
@Service
public class ClueServiceImpl implements ClueService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private ContactsMapper contactsMapper;
    @Autowired
    private ClueRemarkMapper clueRemarkMapper;
    @Autowired
    private CustomerRemarkMapper customerRemarkMapper;
    @Autowired
    private ContactsRemarkMapper contactsRemarkMapper;
    @Autowired
    private ClueActivityRelationMapper clueActivityRelationMapper;
    @Autowired
    private ContactsActivityRelationMapper contactsActivityRelationMapper;
    @Autowired
    private TranMapper tranMapper;



    @Override
    public List<Clue> queryClueListByCondition(Map<String, Object> param) {
        return clueMapper.selectByClueListByCondition(param);
    }

    @Override
    public void saveCreatClue(Clue clue) {
        clueMapper.insertSelective(clue);
    }

    @Override
    public Clue queryClueById(String id) {
        return clueMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveEditClue(Clue clue) {
        clueMapper.updateByPrimaryKeySelective(clue);
    }

    @Override
    public void deleteClueByIds(String[] ids) {
        clueMapper.deleteClueByIds(ids);
    }

    @Override
    public Clue queryClueDetaiById(String id) {
        return clueMapper.selectClueDetaiById(id);

    }

    @Override
    public void saveVConvert(Map<String, Object> param) {
        // 线索的转换
        String clueId = (String) param.get("clueId");
        String amountOfMoney = (String) param.get("amountOfMoney");
        String tradeName = (String) param.get("tradeName");
        String expectedClosingDate = (String) param.get("expectedClosingDate");
        String stage = (String) param.get("stage");
        String activityId = (String) param.get("activityId");
        User sessionUser = (User) param.get("sessionUser");
        Boolean isCreateTransaction = (Boolean) param.get("isCreateTransaction");

        // 获取线索信息
        Clue clue = clueMapper.selectByPrimaryKey(clueId);

        // 生成客户记录
        Customer customer = new Customer();
        customer.setId(UUIDUtils.getUUID());
        customer.setAddress(clue.getAddress());
        customer.setContactSummary(clue.getContactSummary());
        customer.setCreateBy(clue.getCreateBy());
        customer.setCreateTime(clue.getCreateTime());
        customer.setDescription(clue.getDescription());
        customer.setName(clue.getCompany());
        customer.setNextContactTime(clue.getNextContactTime());
        customer.setOwner(clue.getOwner());
        customer.setPhone(clue.getPhone());
        customer.setWebsite(clue.getWebsite());
        customerMapper.insertSelective(customer);
        // 生成联系人记录
        Contacts contacts = new Contacts();
        contacts.setId(UUIDUtils.getUUID());
        contacts.setAddress(clue.getAddress());
        contacts.setAppellation(clue.getAppellation());
        contacts.setContactSummary(sessionUser.getId());
        contacts.setCreateBy(sessionUser.getId());
        contacts.setCreateTime(DateUtil.formatDateTime(new Date()));
        contacts.setCustomerId(customer.getId());
        contacts.setDescription(clue.getDescription());
        contacts.setEmail(clue.getEmail());
        contacts.setFullName(clue.getFullName());
        contacts.setJob(clue.getJob());
        contacts.setMphone(clue.getMphone());
        contacts.setNextContactTime(clue.getNextContactTime());
        contacts.setOwner(clue.getOwner());
        contacts.setSource(clue.getSource());
        contactsMapper.insertSelective(contacts);

        // 获取线索备注列表
        List<ClueRemark> clueRemarkList = clueRemarkMapper.selectClueRemarkList(clueId);
        List<CustomerRemark> customerRemarkList = new ArrayList<>();
        CustomerRemark customerRemark = null;

        List<ContactsRemark> contactsRemarkList = new ArrayList<>();
        ContactsRemark contactsRemark = null;

        for (ClueRemark clueRemark:clueRemarkList){
            customerRemark = new CustomerRemark();
            customerRemark.setId(UUIDUtils.getUUID());
            customerRemark.setCreateBy(sessionUser.getId());
            customerRemark.setCreateTime(DateUtil.formatDateTime(new Date()));
            customerRemark.setCustomerId(customer.getId());
            customerRemark.setNoteContent(clueRemark.getNoteContent());
            customerRemarkList.add(customerRemark);



            contactsRemark = new ContactsRemark();
            contactsRemark.setId(UUIDUtils.getUUID());
            contactsRemark.setCreateBy(sessionUser.getId());
            contactsRemark.setCreateTime(DateUtil.formatDateTime(new Date()));
            contactsRemark.setNoteContent(clueRemark.getNoteContent());
            contactsRemark.setContactsId(contacts.getId());
            contactsRemarkList.add(contactsRemark);


        }


        // 批量生成客户备注
        customerRemarkMapper.insertCustomerRemarkList(customerRemarkList);
        // 批量生成联系人备注
        contactsRemarkMapper.insertContactsRemarkList(contactsRemarkList);

        // 获取线索与市场活动的关系
        List<ClueActivityRelation> clueActivityRelationList =  clueActivityRelationMapper.selectClueActivityRelationListByClueId(clueId);
        // 生成联系人与市场活动的关系
        List<ContactsActivityRelation> contactsActivityRelationList = new ArrayList<>();
        ContactsActivityRelation contactsAcctivityRelation = null;
        for (ClueActivityRelation c :clueActivityRelationList){
            contactsAcctivityRelation = new ContactsActivityRelation();
            contactsAcctivityRelation.setId(UUIDUtils.getUUID());
            contactsAcctivityRelation.setActivityId(c.getActivityId());
            contactsAcctivityRelation.setContactsId(contacts.getId());
            contactsActivityRelationList.add(contactsAcctivityRelation);
        }
        // 批量生成联系人与市场活动关系
        contactsActivityRelationMapper.insertContactsAcitvityRelationList(contactsActivityRelationList);

        // 判断是否产生交易
        if (isCreateTransaction){
            // 生成交易记录
            Tran tran = new Tran();
            tran.setId(UUIDUtils.getUUID());
            tran.setActivityId(activityId);
            tran.setContactsId(contacts.getId());
            tran.setContactSummary(clue.getContactSummary());
            tran.setCreateBy(sessionUser.getId());
            tran.setCreateTime(DateUtil.formatDateTime(new Date()));
            tran.setCustomerId(customer.getId());
            tran.setDescription(clue.getDescription());
            tran.setExpectedDate(expectedClosingDate);
            tran.setNextContactTime(clue.getNextContactTime());
            tran.setMoney(amountOfMoney);
            tran.setName(tradeName);
            // 订单编号：时间戳
            tran.setOrderNo(DateUtil.format(new Date(), "yyyyMMddHHmmssSSS"));
            tran.setOwner(clue.getOwner());
            tran.setSource(clue.getSource());
            tran.setStage(stage);

            tranMapper.insertSelective(tran);
        }

        // 删除线索记录
        clueMapper.deleteByPrimaryKey(clueId);
        // 删除线索备注记录
        clueRemarkMapper.deleteClueRemarkByClueId(clueId);
        // 删除线索与市场活动关系的记录
        clueActivityRelationMapper.deleteClueActivityRelationByClueId(clueId);
    }
}
